package mo.tags.stack;

import java.util.Stack;

public class L962 {

    public int maxWidthRamp(int[] nums) {
        Stack<Integer> stack = new Stack<>();
        int max = 0;
        for (int i = 0; i < nums.length; i++) {
            if (stack.isEmpty() || nums[stack.peek()] > nums[i]) {
                stack.push(i);
            }
        }
        for (int i = nums.length - 1; i >= 0; i--) {
            while (!stack.isEmpty() && nums[stack.peek()] <= nums[i]) {
                Integer pre = stack.pop();
                max = Math.max(max, i-pre);
            }
        }
        return max;
    }

}
